package com.zyf.mt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyf.mt.domain.Goods;
import com.zyf.mt.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/goods/")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    List<Goods> goodsList;

    @RequestMapping("save")
    public String save(HttpServletRequest request, Goods goods){
        goodsService.save(goods);
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        goodsList = goodsService.list(wrapper);
        request.setAttribute("goodsList",goodsList);
        return "/list";
    }

    @RequestMapping("list")
    public String list(HttpServletRequest request){
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        goodsList = goodsService.list(wrapper);
        request.setAttribute("goodsList",goodsList);
        return "/list";
    }

}